package com.sskj.utils;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * 字符串转换 & 编码密码工具
 * 
 */
public class StringUtil {

	public static String replaceStr = "****";

	/**
	 * 
	 * @param str
	 *            输入值
	 * @param separator
	 *            分隔符
	 * @param subLens
	 *            子字符串长度 3,4,5
	 * @return 按指定长度分隔 字符串
	 */
	public static String fmt(String str, String separator, Integer... subLens) {

		if (subLens.length == 0)
			return str;
		if (str == null)
			return "";
		String result = "";
		int pos = 0;
		int strLength = str.length();
		for (int i = 0; i < subLens.length; i++) {
			if (pos >= str.length())
				return result;
			int sl = subLens[i];
			int ep = pos + sl > strLength ? strLength : pos + sl;
			if (i == subLens.length - 1)
				ep = strLength;
			if (ep < strLength) {
				result = result + str.substring(pos, ep) + separator;
			} else {
				result = result + str.substring(pos, ep);
			}
			pos = pos + sl;
		}
		return result;
	}

	/**
	 * 去除空格 及部分控制字符
	 * 
	 * @param str
	 * @return
	 */
	public static String replaceBlank(String str) {

		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}

	/** 判读是否为16进制数据 */
	public static boolean isHex(String str) {

		if (StringUtils.isEmpty(str)) {
			return false;
		}
		String regex = "^[0-9a-fA-F]{1,}$";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(str);
		return matcher.find();
	}

	/**
	 * 字符串日期转换成中文格式日期
	 * 
	 * @param date
	 *            字符串日期 yyyy-MM-dd
	 * @return yyyy年MM月dd日
	 * @throws Exception
	 */
	public static String dateToCnDate(String date) {

		String result = "";
		String[] cnDate = new String[] { "○", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
		String ten = "十";
		String[] dateStr = date.split("-");
		for (int i = 0; i < dateStr.length; i++) {
			for (int j = 0; j < dateStr[i].length(); j++) {
				String charStr = dateStr[i];
				String str = String.valueOf(charStr.charAt(j));
				if (charStr.length() == 2) {
					if (charStr.equals("10")) {
						result += ten;
						break;
					} else {
						if (j == 0) {
							if (charStr.charAt(j) == '1')
								result += ten;
							else if (charStr.charAt(j) == '0')
								result += "";
							else
								result += cnDate[Integer.parseInt(str)] + ten;
						}
						if (j == 1) {
							if (charStr.charAt(j) == '0')
								result += "";
							else
								result += cnDate[Integer.parseInt(str)];
						}
					}
				} else {
					result += cnDate[Integer.parseInt(str)];
				}
			}
			if (i == 0) {
				result += "年";
				continue;
			}
			if (i == 1) {
				result += "月";
				continue;
			}
			if (i == 2) {
				result += "日";
				continue;
			}
		}
		return result;
	}

	public static void _setNvs(String xx, Object... nvs) {

		if (nvs.length == 0) {
			System.out.println("nvs is null");
		} else {
			System.out.println(nvs);
		}
	}

	/**
	 * 使用java正则表达式去掉多余的.与0
	 * 
	 * @param s
	 * @param blw
	 *            如果无小数位时补0字符
	 * @return
	 */
	public static String subZeroAndDot(String s) {

		if (s.indexOf(".") > 0) {
			s = s.replaceAll("0+?$", "");// 去掉多余的0
			s = s.replaceAll("[.]$", "");// 如最后一位是.则去掉
		}
		return s;
	}

	public static boolean contain(String str, String sub) {

		if (str == null || sub == null) {
			return false;
		}
		return str.indexOf(sub) >= 0;
	}

	public static String replace(String str, String target, String replacement) {

		if (StringUtils.isEmpty(str)) {
			return str;
		}
		if (StringUtils.isEmpty(target) || replacement == null) {
			return str;
		}
		if ("*NL*".equals(replacement)) {
			replacement = "\r\n";
		}
		return str.replace(target, replacement);
	}

	public static String replaceAll(String str, String target, String replacement) {

		if (StringUtils.isEmpty(str)) {
			return str;
		}
		if (StringUtils.isEmpty(target) || replacement == null) {
			return str;
		}
		return str.replaceAll(target, replacement);
	}

	public static boolean isPageUrl(String path) {

		if (StringUtils.isEmpty(path)) {
			return false;
		}
		String regex = "(\\.xhtml|\\.seam|\\.action|\\.do|\\.html|\\.asp|\\.aspx)";
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(path);
		return matcher.find();
	}

	public static boolean hasExp(String str) {

		if (StringUtils.isEmpty(str)) {
			return false;
		}
		return str.indexOf("#{") >= 0;
	}

	public static boolean isFullUrl(String str) {

		if (StringUtils.isEmpty(str)) {
			return false;
		}
		return str.indexOf("http://") == 0 || str.indexOf("https://") == 0;
	}

	public static String uRLEncoder(String str) {

		if (str == null) {
			return str;
		}
		try {
			return java.net.URLEncoder.encode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str;
	}

	/***
	 * 
	 * @param params
	 *            要合并的字符串参数
	 * @return 符分隔的合并字符串
	 */
	public static String app(String... params) {

		StringBuilder sb = new StringBuilder("");
		for (int i = 0; i < params.length; i++) {
			if ("*NL*".equals(params[i])) {
				sb.append("\r\n");
			} // Single quotes 单引号
			else if ("*SQ*".equals(params[i])) {
				sb.append("'");
			} // Double quotes 双引号
			else if ("*DQ*".equals(params[i])) {
				sb.append("\"");
			} else {
				sb.append(params[i]);
			}

		}
		return sb.toString();
	}

	/***
	 * 
	 * @param separator
	 *            分隔符
	 * @param params
	 *            要合并的字符串参数
	 * @return 分隔符分隔的合并字符串
	 */
	public static String appSep(String separator, String... params) {

		StringBuilder sb = new StringBuilder("");
		for (int i = 0; i < params.length; i++) {
			sb.append(params[i]);
			if (i < params.length - 1)
				sb.append(separator);
		}
		return sb.toString();
	}

	public static List<String> toList(String value, String separator) {

		List<String> result = new ArrayList<String>();
		if (!StringUtils.isEmpty(value)) {
			String[] as = value.split(separator);
			for (int i = 0; i < as.length; i++) {
				result.add(as[i]);
			}
		}
		return result;
	}

	public static String toStr(List<String> ls, String separator) {

		return toStr(ls, separator, null);
	}

	public static String toStr(List<String> ls, String separator, String nullStr) {

		if (ls == null || ls.isEmpty()) {
			return nullStr;
		}
		String result = "";
		for (int i = 0; i < ls.size(); i++) {
			String xx = ls.get(i);
			result = result + xx;
			if (i < ls.size() - 1) {
				if ("*NL*".equals(separator)) {
					result = result + "\r\n";
				} else {
					result = result + separator;
				}
			}
		}
		return result;
	}

	public static String lsToStr(List<String> list, String separator) {

		if (list == null || list.isEmpty()) {
			return null;
		}
		String result = "";
		for (int i = 0; i < list.size(); i++) {
			String _str = "";
			Object obj = list.get(i);
			if (obj == null)
				continue;
			if (obj instanceof Object[]) {
				Object[] oa = (Object[]) obj;
				_str = oa.toString();
			}
			_str = toStr(obj, "");
			result = result + _str;
			if (i < list.size() - 1) {
				result = result + separator;
			}
		}
		return result;
	}

	public static String toStr(Double o, String fm, String nullStr) {

		DecimalFormat df = new DecimalFormat(fm);
		if (o == null) {
			return nullStr;
		} else {
			return df.format(o);
		}

	}

	public static String toStr(Object o, String nullStr) {

		if (o == null)
			return nullStr;
		if (o instanceof Double) {
			return toStr((Double) o);
		}
		if (o instanceof Calendar) {
			return toStr((Calendar) o);
		}
		if (o instanceof Boolean) {
			return toStr((Boolean) o);
		}
		if (o instanceof String) {
			return toStr((String) o);
		}
		if (o instanceof Integer) {
			return toStr((Integer) o);
		}

		return o.toString();
	}

	public static String toStr(Integer o) {

		return String.valueOf(o);
	}

	public static String toStr(Double o) {

		String result = toStr(o, "#0.000000", "---");
		result = subZeroAndDot(result);
		return result;
	}

	public static String toStr(Calendar o, String nullStr) {

		if (o == null)
			return nullStr;
		return CalendarUtil.convertToString(o);
	}

	public static String toStr(Calendar o) {

		return toStr(o, "****");
	}

	public static String toStr(Boolean o, String[] osa, String nullStr) {

		if (o == null)
			return nullStr;
		if (o) {
			return osa[0];
		} else {
			return osa[1];
		}

	}

	public static String toStr(Boolean o) {

		return toStr(o, new String[] { "真", "假" }, "****");
	}

	public static String toTrueFalse(Boolean o) {

		return toStr(o, new String[] { "true", "false" }, "****");
	}

	public static String toStr(String o) {

		return o;
	}
	

}
